import json
import re
import time
import sys
import requests

from config import getConfigParams
from requestUrl import getEntryIdByUrl
from pyquery import PyQuery as pq
from datetime import datetime
from logs import saveLog
from formatContent import removeSrcParams, formatContent

# 获取配置参数
configParams = getConfigParams(sys.argv)
url = configParams['real_url']
env = configParams['env']

headers = {
    'referer': 'https://juejin.im/',
}

# 获取html文本doc，得到pq对象
response = requests.get(url=url, headers=headers)
doc = pq(response.text)
# 封面图
coverUrl = ''
if doc('.article-content img'):
    firstImg = doc(doc('.article-content img')[0])
    coverUrl = firstImg.attr('src')
    if not coverUrl:
        coverUrl = firstImg.attr('data-src')
    if coverUrl.find('https') < 0:
        coverUrl = 'https:' + coverUrl

if doc('div.thumb'):
    coverUrl = doc('div.thumb').attr('data-src')
# if json.loads(doc(doc('script')[0]).text())['image']:
#     coverUrl = json.loads(doc(doc('script')[0]).text())['image'][0]

# 标题
title = doc('.article-title').text()

# 文章正文
# 并将图片？后的参数去掉，否则无图
articleContent = removeSrcParams(doc('.article-content'))

# 文章作者
name = re.findall("<meta.*?itemprop=\"name\" content=\"([^\"]+)\"/>", doc.html())[0]
authorName = name

# 时间：Y年m月d日 或 m月d日
# 格式化为Y-m-d
# 发布时间
publicTime = int(time.mktime(datetime.strptime(doc('time').attr('datetime'), "%Y-%m-%dT%H:%M:%S.%fZ").timetuple()))

# 去【李世泉】那里过滤标签
content = formatContent(articleContent.html(), env=env)

result = {
    'entry_id': doc('article').attr('data-entry-id'),
    'not_format_content': articleContent.html(),
    'content': content,
    'author_name': authorName,
    'cover_url': coverUrl,
    'public_time': publicTime,
    'description': '',
    'title': title,
}
logData = {
    'params': sys.argv,
    'result': result
}
saveLog(json.dumps(logData, ensure_ascii=False))
# 结果输出
print(json.dumps(result, ensure_ascii=False))
